package com.ztr.test.ip;

import java.io.IOException;
import java.nio.file.Files;  
import java.nio.file.Path;  
import java.nio.file.Paths;  
import java.util.regex.Matcher;  
import java.util.regex.Pattern;  
  
public class NginxConfReader {  
  
    public static void main(String[] args) {  
        Path path = Paths.get("D:\\nginx\\conf\\nginx.conf");  
        String content = null;  
        try {  
            content = new String(Files.readAllBytes(path));  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
  
        if (content != null) {  
            Pattern listenPattern = Pattern.compile("listen\\s+(\\d+);");  
            Pattern serverNamePattern = Pattern.compile("server_name\\s+(.+);");  
  
            Matcher listenMatcher = listenPattern.matcher(content);  
            if (listenMatcher.find()) {  
                System.out.println("listen: " + listenMatcher.group(1));  
            }  
  
            Matcher serverNameMatcher = serverNamePattern.matcher(content);  
            if (serverNameMatcher.find()) {  
                System.out.println("server_name: " + serverNameMatcher.group(1));  
            }  
        }  
    }  
}